use std::mem;
use windows::Win32::Foundation::GetLastError;
use windows::Win32::System::SystemInformation::{GlobalMemoryStatusEx, MEMORYSTATUSEX};
static mut COUNTER: u32 = 0;
fn add_to_count(inc: u32) {
    unsafe {
        COUNTER += inc;
    }
}
fn main() {
    println!("Hello, world!");
    let mut num = 5;
    let r1 = &num as *const i32;
    let r2 = &mut num as *mut i32;
    let address = 0x012345usize;
    let r = address as *const i32;
    add_to_count(3);
    unsafe {
        println!("COUNTER: {}", COUNTER);
    }
    unsafe {
        println!("r1 is: {}", *r1);
        println!("r2 is: {}", *r2);
    }

    unsafe {
        let mut memory_status = MEMORYSTATUSEX {
            dwLength: mem::size_of::<MEMORYSTATUSEX>() as u32,
            ..Default::default()
        };

        // 调用GlobalMemoryStatusEx函数填充内存状态
        let result = unsafe { GlobalMemoryStatusEx(&mut memory_status) };

        if let Ok(val) = result {
            // 计算已使用的物理内存
            let used_phys_memory = memory_status.ullTotalPhys - memory_status.ullAvailPhys;
            // 计算已使用的页面文件
            let used_page_file = memory_status.ullTotalPageFile - memory_status.ullAvailPageFile;
            // 计算已使用的虚拟内存
            let used_virtual_memory = memory_status.ullTotalVirtual - memory_status.ullAvailVirtual;

            println!(
                "Total Physical Memory: {:.2} GB",
                memory_status.ullTotalPhys as f64 / (1024.0 * 1024.0 * 1024.0)
            );
            println!(
                "Used Physical Memory: {:.2} GB",
                used_phys_memory as f64 / (1024.0 * 1024.0 * 1024.0)
            );
            println!(
                "Available Physical Memory: {:.2} GB",
                memory_status.ullAvailPhys as f64 / (1024.0 * 1024.0 * 1024.0)
            );

            println!(
                "Total Page File: {:.2} GB",
                memory_status.ullTotalPageFile as f64 / (1024.0 * 1024.0 * 1024.0)
            );
            println!(
                "Used Page File: {:.2} GB",
                used_page_file as f64 / (1024.0 * 1024.0 * 1024.0)
            );
            println!(
                "Available Page File: {:.2} GB",
                memory_status.ullAvailPageFile as f64 / (1024.0 * 1024.0 * 1024.0)
            );

            println!(
                "Total Virtual Memory: {:.2} GB",
                memory_status.ullTotalVirtual as f64 / (1024.0 * 1024.0 * 1024.0)
            );
            println!(
                "Used Virtual Memory: {:.2} GB",
                used_virtual_memory as f64 / (1024.0 * 1024.0 * 1024.0)
            );
            println!(
                "Available Virtual Memory: {:.2} GB",
                memory_status.ullAvailVirtual as f64 / (1024.0 * 1024.0 * 1024.0)
            );
        }
    }
}
